app.controller("z-forgetPassword2-c", ['$scope', '$location','$rootScope', '$z-http', '$sce', 'z-forgetPassword-s','z-employee-s','z-login-s','z-init-s','z-SessionTimer-s','$z-http','ZModel',"z-password-s","tokenService","Md5",'$filter',
    function ($scope, $location, $rootScope, $zhttp, $sce, Service, EmployeeService, LoginService,InitService,SessionTimer,$zhttp,ZModel,PasswordService,tokenService,Md5,$filter) {
	$scope.loginModel = {};
	$scope.loginModel.loginName = localStorage.getItem('loginName');
	$scope.loginModel.password = localStorage.getItem('password');
	$scope.loginModel.alertDangerText = "";
	$scope.loginModel.alertDangerPassText="";
	$scope.loginModel.mail="";
	$scope.loginModel.CSRFToken="";
	$scope.loginModel.hideAlert = true;
	$scope.loginModel.passwordType=false;
	$scope.loginModel.logonType= true;
	$scope.loginModel.hideAlertPass=true;
	$scope.loginModel.forgetType=false;

	//保存更改密码
	$scope.loginModel.saveOn= function() {
		var check = /^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[^\sA-Za-z0-9])\S{8,}$/;
		if($scope.loginModel.newPassword == "" || $scope.loginModel.newPassword == undefined){
			$rootScope.alertWarning("新密码不能为空！");
		}else if($scope.loginModel.confirmPassword == "" || $scope.loginModel.confirmPassword == undefined){
			$rootScope.alertWarning("请再次输入密码！");
		}else if($scope.loginModel.confirmPassword != $scope.loginModel.newPassword){
			$rootScope.alertWarning("两次输入的新密码不一致！");
		}else if(!check.test($scope.loginModel.newPassword) || !check.test($scope.loginModel.confirmPassword)){
			$rootScope.alertWarning("密码格式不正确(须含有大小写字母，数字，特殊符号，长度为8位及8位以上)！");
		}else{
			
			var navModel = ZModel.getInstance().navModel;
			
			var search=$location.search()['key'];
			//验证是否存在KEY
			if(angular.isUndefined(search)){
				//不存在
				//currentUser();
				$rootScope.alertWarning("无效的链接地址！");
				
				
			}else{
				//验证key是否使用，调用方法验证KEY
				Service.checkKey({
							key:search
						},
						function(data) {
							if(data){
								//有效
							}else{
								alert(data);
								$rootScope.alertWarning("无效的链接地址！");
							}
						},
						function() {
							
						});
			}
			$scope.changePassObj={};
//			$scope.changePassObj.loginName=$scope.loginModel.loginName;
			$scope.changePassObj.newPassword=$scope.loginModel.newPassword;
			$scope.isLoading = true;
			Service.doResetPassword({
				key:search,
				pw1:$scope.loginModel.newPassword,
				pw2:$scope.loginModel.confirmPassword
			},function() {
				$scope.isLoading = false;
				$rootScope.alertConfirm("密码已修改成功，是否重新登录",function() {
					$scope.loginModel.hideAlertPass=true;
					$scope.loginModel.alertDangerPassText = "";
					$scope.loginModel.passwordType=false;
					$scope.loginModel.logonType= true;
					
//					$rootScope.loadUrl = url;
//					navModel.prevPage = $route.current.$$route.title;
//					navModel.prevUrl = $location.$$url;
//					//记录作为要访问的页面，并跳转到登陆页
//					$location.path("/login");
//					$location.hash("");
//					$location.replace();
					
					$scope.loginModel.loginGo();
				},function() {
					
				});
			},function(error) {
				$scope.isLoading = false;
				$scope.loginModel.oldPassword="";
				$scope.loginModel.hideAlertPass=false;
				$scope.loginModel.alertDangerPassText = error.message;
			});
		}
	};
		
	$scope.$watch('loginModel.loginName',function(newValue,oldValue) {
		if(newValue!=""){
			$scope.loginModel.hideAlert = true;
		}
	});
	
	$scope.$watch('loginModel.password',function(newValue,oldValue) {
		if(newValue!=""){
			$scope.loginModel.hideAlert = true;
		}
	});
	
	//登陆跳转
	$scope.loginModel.loginGo = function() {
		/*Md5.hex_md5($scope.loginModel.password)*/
		LoginService.login(
				$scope.loginModel.loginName,
				$scope.loginModel.password, 
				$scope.loginModel.CSRFToken,
				function(data) {
					//设置全局变量
					tokenService(data.token, true); 
					if($scope.loginModel.remberPassword){
						localStorage.setItem('loginName',$scope.loginModel.loginName);
						localStorage.setItem('password',$scope.loginModel.password);
						localStorage.setItem('remberPassword','true');
					}else{
						localStorage.removeItem('loginName');
						localStorage.removeItem('password');
						localStorage.removeItem('remberPassword');
					}
					//执行初始化服务
					InitService.init(function(data) {
						SessionTimer.listenTimeout(function() {$zhttp.post('session/checkTimeout.do');},data,function() {
							//alert("提示会话超时，任何操作查询及保存将无效，询问是否重新登陆，是则调回登录页，否则无任何操作关闭提示");
							
//									$rootScope.alertConfirm("会话已超时，任何操作查询及保存将无效，是否重新登录?",function() {
//										//记录作为要访问的页面，并跳转到登陆页
//										$rootScope.currentUser = null;
//										ZModel.getInstance().navModel.beforeLoginUrl = $location.$$url;
//										
//										$location.path("/login");
//										$location.hash("");
//										$location.replace();
//									},function() {
//										SessionTimer.listenTimeout();
//									});
							
							var _confirm = window.confirm("会话已超时，任何操作查询及保存将无效，是否重新登录?");
							if(_confirm){
									//记录作为要访问的页面，并跳转到登陆页
									$rootScope.currentUser = null;
									ZModel.getInstance().navModel.beforeLoginUrl = $location.$$url;
									
									$location.path("/login");
									$location.hash("");
									$location.replace();
							} else {
								SessionTimer.listenTimeout();
							}
						});
						$location.path("/login");
						$location.hash("");
						$location.replace();
					});
				}, function(data) {
						$scope.loginModel.alertDangerText = data.message;
						$scope.loginModel.password = "";
						$scope.loginModel.hideAlert = false;
				});
	};
}]);

			
			